package editor;

import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
 * This is a sensible implementation of the FileFilter class, so that one can just specify the extensions to accept.
 * @author Neil Dickson
 */
public class CustomFileFilter extends FileFilter {
	public final String		description;
	public final String[]	extensions;

	public CustomFileFilter(String[] extensions,String description) {
		this.description = description;
		this.extensions = extensions;
	}

	public boolean accept(File file) {
		if (file.isDirectory()) {
			return true;
		}
		for (String ext : extensions) {
			if (ext.equals("*")) {
				return true;
			}
			String name = file.getName();
			int length = name.length();
			int extLength = ext.length();
			if (length>extLength+1 && name.charAt(length-extLength-1)=='.' && name.regionMatches(true,length-extLength,ext,0,extLength)) {
				return true;
			}
		}
		return false;
	}

	public String getDescription() {
		return description;
	}
}
